{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Value after Insertion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxValue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #插入后的最大值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个非常大的整数 <code>n</code> 和一个整数数字 <code>x</code> ，大整数 <code>n</code> 用一个字符串表示。<code>n</code> 中每一位数字和数字 <code>x</code> 都处于闭区间 <code>[1, 9]</code> 中，且 <code>n</code> 可能表示一个 <strong>负数</strong> 。</p>\n",
    "\n",
    "<p>你打算通过在 <code>n</code> 的十进制表示的任意位置插入 <code>x</code> 来 <strong>最大化</strong> <code>n</code> 的 <strong>数值</strong> ​​​​​​。但 <strong>不能</strong> 在负号的左边插入 <code>x</code> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，如果 <code>n = 73</code> 且 <code>x = 6</code> ，那么最佳方案是将 <code>6</code> 插入 <code>7</code> 和 <code>3</code> 之间，使 <code>n = 763</code> 。</li>\n",
    "\t<li>如果 <code>n = -55</code> 且 <code>x = 2</code> ，那么最佳方案是将 <code>2</code> 插在第一个 <code>5</code> 之前，使 <code>n = -255</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回插入操作后，用字符串表示的 <code>n</code> 的最大值。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = \"99\", x = 9\n",
    "<strong>输出：</strong>\"999\"\n",
    "<strong>解释：</strong>不管在哪里插入 9 ，结果都是相同的。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = \"-13\", x = 2\n",
    "<strong>输出：</strong>\"-123\"\n",
    "<strong>解释：</strong>向 n 中插入 x 可以得到 -213、-123 或者 -132 ，三者中最大的是 -123 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= n.length <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 <= x <= 9</code></li>\n",
    "\t<li><code>n</code>​​​ 中每一位的数字都在闭区间 <code>[1, 9]</code> 中。</li>\n",
    "\t<li><code>n</code> 代表一个有效的整数。</li>\n",
    "\t<li>当 <code>n</code> 表示负数时，将会以字符 <code>'-'</code> 开始。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-value-after-insertion](https://leetcode.cn/problems/maximum-value-after-insertion/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-value-after-insertion](https://leetcode.cn/problems/maximum-value-after-insertion/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"99\"\\n9', '\"-13\"\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, val=0, next=None):\n",
    "#         self.val = val\n",
    "#         self.next = next\n",
    "class Solution:\n",
    "    def pairSum(self, head: Optional[ListNode]) -> int:\n",
    "        if not head:\n",
    "            return 0\n",
    "        maxValue = float('-inf')\n",
    "        slow = fast = head\n",
    "        while fast and fast.next:\n",
    "            fast = fast.next.next\n",
    "            slow = slow.next\n",
    "        fast = head\n",
    "        slow = self.reverseList(slow)\n",
    "        while slow:\n",
    "            maxValue = max(fast.val + slow.val, maxValue)\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        return maxValue\n",
    "    def reverseList(self, head:ListNode) -> ListNode:\n",
    "        pre, cur = None, head\n",
    "        while cur:\n",
    "            temp = cur.next\n",
    "            cur.next = pre\n",
    "            pre = cur\n",
    "            cur = temp\n",
    "        return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        if n[0] == \"-\":\n",
    "            for i, j in enumerate(n):\n",
    "                if j > x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "        else:\n",
    "            for i, j in enumerate(n):\n",
    "                if j < x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "        return n+x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        neg = n[0] == '-'\n",
    "        for c in n:\n",
    "            if c != x and (c < x) ^ neg:\n",
    "                return n.replace(c, x + c, 1)\n",
    "        return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        if n[0] == '-':\n",
    "            for c in n[1:]:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        else:\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        # 正数：大于等于x的都得放前面，循环直到遇到第一个小于x的\n",
    "        # 负数：小于等于x的都得放前面，循环直到遇到第一个大于x的\n",
    "        x = str(x)\n",
    "        if n[0] != '-':\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x\n",
    "        else:\n",
    "            for c in n:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        for c in n:\n",
    "            if c != x and (c < x) ^ (n[0] == '-'):\n",
    "                return n.replace(c, x + c, 1)\n",
    "        return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        m = len(n)\n",
    "        if n[0] == '-':\n",
    "            i = 1\n",
    "            while i < m and n[i] <= x:\n",
    "                i += 1\n",
    "        else:\n",
    "            i = 0\n",
    "            while i < m and n[i] >= x:\n",
    "                i += 1\n",
    "        return n[:i] + x + n[i:] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        flag=0\n",
    "        if n[0]=='-':\n",
    "            flag=1\n",
    "            n=n[1:]\n",
    "        for i,n1 in enumerate(n):         \n",
    "            if int(n1)<x and flag==0:\n",
    "                pre=n[:i]\n",
    "                suf=str(x)+n[i:]\n",
    "                return pre+suf\n",
    "            elif int(n1)>x and flag==1:\n",
    "                pre=\"-\"+n[:i]\n",
    "                suf=str(x)+n[i:]\n",
    "                return pre+suf\n",
    "        return n+str(x) if flag==0 else '-'+n+str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        # 正数：大于等于x的都得放前面，循环直到遇到第一个小于x的\n",
    "        # 负数：小于等于x的都得放前面，循环直到遇到第一个大于x的\n",
    "        x = str(x)\n",
    "        if n[0] == '-':\n",
    "            for c in n[1:]:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x\n",
    "        else:\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        # 正数：大于等于x的都得放前面，循环直到遇到第一个小于x的\n",
    "        # 负数：小于等于x的都得放前面，循环直到遇到第一个大于x的\n",
    "        x = str(x)\n",
    "        if n[0] == '-':\n",
    "            for c in n[1:]:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        else:\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        return n + x\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        direction = 1\n",
    "        base_ord = ord(\"0\")\n",
    "        i = 0\n",
    "\n",
    "        while i < len(n):\n",
    "            nn = n[i]\n",
    "\n",
    "            if nn == \"-\":\n",
    "                direction = -1\n",
    "                i += 1\n",
    "            else:\n",
    "                if (ord(nn) - base_ord) * direction >= x * direction:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    break\n",
    "\n",
    "        return n[:i] + str(x) + n[i:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if x < int(n[i]):\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if x > int(n[i]):\n",
    "                    return n[:i] + str(x) + n[i:] \n",
    "        return n + str(x)\n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] != \"-\":\n",
    "            temp = \"\"\n",
    "            for i in range(len(n)):\n",
    "                if int(n[i]) < x:\n",
    "                    temp = n[:i] + str(x) + n[i:]\n",
    "                    return temp\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(1, len(n)):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        direction = 1\n",
    "        base_ord = ord(\"0\")\n",
    "        i = 0\n",
    "\n",
    "        while i < len(n):\n",
    "            nn = n[i]\n",
    "\n",
    "            if nn == \"-\":\n",
    "                direction = -1\n",
    "                i += 1\n",
    "            else:\n",
    "                if (ord(nn) - base_ord) * direction >= x * direction:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    break\n",
    "\n",
    "        return n[:i] + str(x) + n[i:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        if n[0] == \"-\":\n",
    "            for index in range(len(n)):\n",
    "                if n[index] > x:\n",
    "                    return n[:index] + x + n[index:]\n",
    "\n",
    "        else:\n",
    "            for index in range(len(n)):\n",
    "                if n[index] < x:\n",
    "                    return n[:index] + x + n[index:]\n",
    "\n",
    "        return n + x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        s ,f = 0,1\n",
    "        # if l <= 0:\n",
    "        #     return 0\n",
    "        if n[0]=='-':\n",
    "            s ,f = 1,-1\n",
    "        #     for i in range(1,l):\n",
    "        #         if int(n[i]) > x:\n",
    "        #             return n[:i] + str(x) +n[i:]\n",
    "        #     return n + str(x)\n",
    "        # else:\n",
    "\n",
    "        #     for i in range(l):\n",
    "        #         if int(n[i]) < x:\n",
    "        #             return n[:i] + str(i) + n[i:]\n",
    "        #     return n +str(x)\n",
    "\n",
    "        for i in range(s,l):\n",
    "            if x*f > int(n[i])*f :\n",
    "                return n[:i] + str(x) +n[i:]\n",
    "        return n + str(x)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        if n[0] == '-':\n",
    "            for c in n[1:]:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        else:\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "        return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, len_n = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        \n",
    "        for i in range(start, len_n):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, s: str, x: int) -> str:\n",
    "        N = len(s)\n",
    "        if s[0] == '-':\n",
    "            for i in range(1, N):\n",
    "                if int(s[i]) > x:\n",
    "                    res = s[:i] + str(x) + s[i:]\n",
    "                    return res\n",
    "            return s + str(x)\n",
    "        else:\n",
    "            for i in range(N):\n",
    "                if int(s[i]) < x:\n",
    "                    res = s[:i] + str(x) + s[i:]\n",
    "                    return res\n",
    "            return s + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        m = len(n)\n",
    "        x = str(x)\n",
    "        if n[0] != '-':\n",
    "            i = 0\n",
    "            while i < m:\n",
    "                if n[i] < x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "                i += 1\n",
    "            return n + x\n",
    "        else:\n",
    "            i = 1\n",
    "            while i < m:\n",
    "                if n[i] > x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "                i += 1\n",
    "            return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == '-':\n",
    "            for i, j in enumerate(n[1:], 1):\n",
    "                if j > str(x):\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i, j in enumerate(n):\n",
    "                if j < str(x):\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == '-':\n",
    "            index = 0\n",
    "            for i in range(1,len(n)):\n",
    "                if int(n[i]) > x:\n",
    "                    index = i\n",
    "                    break\n",
    "            if index == 0:\n",
    "                return n+str(x)\n",
    "            else:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        else:\n",
    "            index = -1\n",
    "            for i in range(0,len(n)):\n",
    "                if int(n[i]) < x:\n",
    "                    index = i\n",
    "                    break\n",
    "            if index == -1:\n",
    "                return n+str(x)\n",
    "            elif index == 0:\n",
    "                return str(x) + n\n",
    "            else:\n",
    "                return n[:i] + str(x) + n[i:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        i, x, m = n[0] == '-', str(x), len(n)\n",
    "        if i:\n",
    "            while i < m and n[i] <= x:\n",
    "                i += 1\n",
    "        else:\n",
    "            while i < m and n[i] >= x:\n",
    "                i += 1\n",
    "        return n[:i] + x + n[i:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        if n[0] == \"-\":\n",
    "            for i, j in enumerate(n):\n",
    "                if j > x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "        else:\n",
    "            for i, j in enumerate(n):\n",
    "                if j < x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "        return n+x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        \n",
    "        nums=len(n)\n",
    "        if n[0]!='-':\n",
    "            for i in range(nums):\n",
    "                if int(n[i])<x:\n",
    "                    return n[:i]+str(x)+n[i:]\n",
    "            return n+str(x)\n",
    "        else:\n",
    "            for i in range(1,nums):\n",
    "                if int(n[i])>x:\n",
    "                    return '-'+n[1:i]+str(x)+n[i:]\n",
    "            return n+str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        ln = len(n)\n",
    "        if n[0] != '-':\n",
    "            for i in range(ln):\n",
    "                if n[i] < x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "            return n + x\n",
    "        else:\n",
    "            for i in range(1, ln):\n",
    "                if n[i] > x:\n",
    "                    return n[:i] + x + n[i:]\n",
    "            return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = str(x)\n",
    "        i = 0\n",
    "        if n[0] != '-':\n",
    "            while i < len(n) and n[i] >= x:\n",
    "                i += 1\n",
    "        else:\n",
    "            i = 1\n",
    "            while i < len(n) and n[i] <= x:\n",
    "                i += 1\n",
    "        return n[:i] + x + n[i:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start,flag,ln = 0,1,len(n)\n",
    "        if n[0] == '-':\n",
    "            start,flag = 1,-1\n",
    "        for i in range(start,ln):\n",
    "            if x*flag>int(n[i])*flag:\n",
    "                return n[:i]+str(x)+n[i:]\n",
    "        return n+str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] != \"-\":\n",
    "            temp = \"\"\n",
    "            for i in range(len(n)):\n",
    "                if int(n[i]) < x:\n",
    "                    temp = n[:i] + str(x) + n[i:]\n",
    "                    return temp\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            temp = \"\"\n",
    "            for i in range(1, len(n)):\n",
    "                if int(n[i]) > x:\n",
    "                    temp = n[:i] + str(x) + n[i:]\n",
    "                    return temp\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        # 正数：大于等于x的都得放前面，循环直到遇到第一个小于x的\n",
    "        # 负数：小于等于x的都得放前面，循环直到遇到第一个大于x的\n",
    "        x = str(x)\n",
    "        if n[0] != '-':\n",
    "            for c in n:\n",
    "                if c < x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x\n",
    "        else:\n",
    "            for c in n[1:]:\n",
    "                if c > x:\n",
    "                    return n.replace(c, x + c, 1)\n",
    "            return n + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        \n",
    "        if '-' != n[0]:\n",
    "            for i in range(len(n)):\n",
    "                if x > int(n[i]):\n",
    "                    return n[:i] + str(x) + n[i:]     \n",
    "        else:\n",
    "            for i in range(1, len(n)):\n",
    "                if x < int(n[i]):\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, s: str, x: int) -> str:\n",
    "        N = len(s)\n",
    "        if s[0] == '-':\n",
    "            for i in range(1, N):\n",
    "                if int(s[i]) > x:\n",
    "                    res = s[:i] + str(x) + s[i:]\n",
    "                    return res\n",
    "            return s + str(x)\n",
    "        else:\n",
    "            for i in range(N):\n",
    "                if int(s[i]) < x:\n",
    "                    res = s[:i] + str(x) + s[i:]\n",
    "                    return res\n",
    "            return s + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maxValue(self, n: str, x: int) -> str:\r\n",
    "        start, flag, ln = 0, 1, len(n)\r\n",
    "        if n[0] == '-':\r\n",
    "            start, flag = 1, -1\r\n",
    "        for i in range(start, ln):\r\n",
    "            if x * flag > int(n[i]) * flag:\r\n",
    "                return n[:i] + str(x) + n[i:]\r\n",
    "        return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start,flag,ln = 0,1,len(n)\n",
    "        if n[0] == '-':\n",
    "            start,flag = 1,-1\n",
    "        for i in range(start,ln):\n",
    "            if x*flag>int(n[i])*flag:\n",
    "                return n[:i]+str(x)+n[i:]\n",
    "        return n+str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        start, flag, ln = 0, 1, len(n)\n",
    "        if n[0] == '-':\n",
    "            start, flag = 1, -1\n",
    "        for i in range(start, ln):\n",
    "            if x * flag > int(n[i]) * flag:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        for i in range(l):\n",
    "            if int(n[i]) < x:\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        insert_i = len(n)\n",
    "        if n[0]  == '-':\n",
    "            for i in range(1, len(n)):\n",
    "                if x < int(n[i]):\n",
    "                    insert_i = i\n",
    "                    break\n",
    "        else:\n",
    "            for i in range(len(n)):\n",
    "                if x > int(n[i]): \n",
    "                    insert_i = i\n",
    "                    break\n",
    "        print(insert_i)\n",
    "        return n[:insert_i] + str(x) + n[insert_i:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] != \"-\":\n",
    "            temp = \"\"\n",
    "            for i in range(len(n)):\n",
    "                if int(n[i]) < x:\n",
    "                    temp = n[:i] + str(x) + n[i:]\n",
    "                    return temp\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            temp = \"\"\n",
    "            for i in range(1, len(n)):\n",
    "                if int(n[i]) > x:\n",
    "                    temp = n[:i] + str(x) + n[i:]\n",
    "                    return temp\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if(n[0] == '-'):\n",
    "            idx = None\n",
    "            for i in range(1,len(n)):\n",
    "                if(x < int(n[i])):\n",
    "                    idx = i \n",
    "                    break\n",
    "            if(idx == None):\n",
    "                return n + str(x)\n",
    "            else:\n",
    "                return n[:idx] + str(x) + n[idx:]\n",
    "        else:\n",
    "            idx = None\n",
    "            for i in range(len(n)):\n",
    "                if(x > int(n[i])):\n",
    "                    idx = i \n",
    "                    break \n",
    "            if(idx == None):\n",
    "                return n + str(x)\n",
    "            else:\n",
    "                return n[:idx] + str(x) + n[idx:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == \"-\":\n",
    "            m = len(n)\n",
    "            for i in range(1, m):\n",
    "                if n[i] > str(x):\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n+str(x)\n",
    "        \n",
    "        m = len(n)\n",
    "        for i in range(m):\n",
    "            if n[i] < str(x):\n",
    "                return n[:i] + str(x) + n[i:]\n",
    "        return n+str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, len(n)):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(len(n)):\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        # 若 n 是正数\n",
    "        if n[0] != '-':\n",
    "            for i in range(len(n)):\n",
    "                # 找到第一个比 x 小的数字，将 x 插入其前面\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            # 如果没有找到，就在末尾添加\n",
    "            return n + str(x)\n",
    "        # 若 n 是负数\n",
    "        else:\n",
    "            for i in range(1, len(n)):\n",
    "                # 找到第一个比 x 大的数字，将 x 插入其前面\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            # 如果没有找到，就在末尾添加\n",
    "            return n + str(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] != '-':\n",
    "            for i,num in enumerate(n):\n",
    "                if int(num) < x:\n",
    "                    return (n[:i] + str(x) + n[i:])\n",
    "            return (n + str(x))\n",
    "        else:\n",
    "            for i,num in enumerate(n[1:]):\n",
    "                if int(num) > x:\n",
    "                    return (n[:(i+1)] + str(x) + n[(i+1):])\n",
    "            return (n + str(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        x = ord(str(x))\n",
    "        nn = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, nn):\n",
    "                if ord(n[i]) > x:\n",
    "                    return n[:i]+chr(x)+n[i:]\n",
    "        else:\n",
    "            for i in range(nn):\n",
    "                if ord(n[i]) < x:\n",
    "                    return n[:i]+chr(x)+n[i:]\n",
    "        return n+chr(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = len(n)\n",
    "        if n[0] == '-':\n",
    "            for i in range(1, l):\n",
    "                if int(n[i]) > x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if int(n[i]) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n + str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        flag=0\n",
    "        res=''\n",
    "        if n[0]=='-':\n",
    "            res+='-'\n",
    "            for i in range(1,len(n)):\n",
    "                if flag==0 and int(n[i])>x:\n",
    "                    flag=1\n",
    "                    res+=str(x)\n",
    "                res+=n[i]\n",
    "            if flag==0:\n",
    "                res+=str(x)\n",
    "        else:\n",
    "            for i in range(len(n)):\n",
    "                if flag==0 and int(n[i])<x:\n",
    "                    flag=1\n",
    "                    res+=str(x)\n",
    "                res+=n[i]\n",
    "            if flag==0:\n",
    "                res+=str(x)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == \"-\":\n",
    "            r = \"-\"\n",
    "            i = 1\n",
    "            while i < len(n):\n",
    "                if x >= int(n[i]):\n",
    "                    r += n[i]\n",
    "                else:\n",
    "                    r += str(x)\n",
    "                    r += n[i:]\n",
    "                    break\n",
    "                    \n",
    "                i += 1\n",
    "\n",
    "            if i == len(n):\n",
    "                r += str(x)\n",
    "                \n",
    "        else:\n",
    "            r = \"\"\n",
    "            i = 0\n",
    "            while i < len(n):\n",
    "                if x > int(n[i]):\n",
    "                    r += str(x)\n",
    "                    r += n[i:]\n",
    "                    break\n",
    "                else:\n",
    "                    r += n[i]\n",
    "                \n",
    "                i += 1\n",
    "            \n",
    "            if i == len(n):\n",
    "                r += str(x)\n",
    "        \n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        if n[0] == \"-\":\n",
    "            i = 1\n",
    "            while i < len(n) and int(n[i]) <= x:\n",
    "                i += 1\n",
    "            n = list(n)\n",
    "            n.insert(i, str(x))\n",
    "            return \"\".join(n)\n",
    "        else:\n",
    "            i = 0\n",
    "            while i < len(n) and int(n[i]) >= x:\n",
    "                i += 1\n",
    "            n = list(n)\n",
    "            n.insert(i, str(x))\n",
    "            return \"\".join(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        num_list = list(n)\n",
    "        if num_list[0] == '-':\n",
    "            for i in range( 1, len(num_list)):\n",
    "                if int( num_list[i] ) > x:\n",
    "                    return n[:i] + str(x) + n[i:] \n",
    "            return n+str(x)\n",
    "        else:\n",
    "            for i in range( 0, len(num_list) ):\n",
    "                if int( num_list[i] ) < x:\n",
    "                    return n[:i] + str(x) + n[i:]\n",
    "            return n+str(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        ans = \"\"\n",
    "        if n[0] == \"-\":\n",
    "            ans+=\"-\"\n",
    "            flag = 0\n",
    "            for c in n[1:]:\n",
    "                if flag==0 and int(c)>int(x):\n",
    "                    ans += str(x)\n",
    "                    flag = 1\n",
    "                ans += c\n",
    "            if flag==0:\n",
    "                ans += str(x)\n",
    "            return ans\n",
    "\n",
    "        flag = 0\n",
    "        for c in n:\n",
    "            if flag==0 and int(c)<int(x):\n",
    "                ans += str(x)\n",
    "                flag = 1\n",
    "            ans += c\n",
    "        if flag==0:\n",
    "            ans += str(x)\n",
    "        return ans                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        n = list(n)\n",
    "        if n[0] == '-':\n",
    "            size = len(n)\n",
    "            for i in range(1,size):\n",
    "                if int(n[i]) > x:\n",
    "                    n.insert(i,str(x))\n",
    "                    return ''.join(n)\n",
    "            n.append(str(x))\n",
    "            return ''.join(n)\n",
    "        else:\n",
    "            size = len(n)\n",
    "            for i in range(size):\n",
    "                if int(n[i]) < x:\n",
    "                    n.insert(i,str(x))\n",
    "                    return ''.join(n)\n",
    "            n.append(str(x))\n",
    "            return ''.join(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        n = list(n)\n",
    "        if n[0] == '-':\n",
    "            size = len(n)\n",
    "            for i in range(1,size):\n",
    "                if int(n[i]) > x:\n",
    "                    n.insert(i,str(x))\n",
    "                    return ''.join(n)\n",
    "            n.append(str(x))\n",
    "            return ''.join(n)\n",
    "        else:\n",
    "            size = len(n)\n",
    "            for i in range(size):\n",
    "                if int(n[i]) < x:\n",
    "                    n.insert(i,str(x))\n",
    "                    return ''.join(n)\n",
    "            n.append(str(x))\n",
    "            return ''.join(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        ans = []\n",
    "        if n[0] == \"-\":\n",
    "            ans.append(\"-\")\n",
    "            flag = 0\n",
    "            for c in n[1:]:\n",
    "                if flag==0 and int(c)>int(x):\n",
    "                    ans.append(str(x))\n",
    "                    flag = 1\n",
    "                ans.append(c)\n",
    "            if flag==0:\n",
    "                ans.append(str(x))\n",
    "            return \"\".join(ans)\n",
    "        \n",
    "\n",
    "        flag = 0\n",
    "        for c in n:\n",
    "            if flag==0 and int(c)<int(x):\n",
    "                ans.append(str(x))\n",
    "                flag = 1\n",
    "            ans.append(c)\n",
    "        if flag==0:\n",
    "            ans.append(str(x))\n",
    "\n",
    "        return \"\".join(ans)                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        length=len(n)\n",
    "        lst=[]\n",
    "        for i in range(length):\n",
    "            lst.append(n[i])\n",
    "        if n[0]!=\"-\":\n",
    "            a=0\n",
    "            for j in range(length):\n",
    "                if int(lst[j])<x:\n",
    "                    lst.insert(j,str(x))\n",
    "                    a+=1\n",
    "                    break\n",
    "            if a==0:\n",
    "                lst.append(str(x))\n",
    "        else:\n",
    "            b=0\n",
    "            for k in range(1,length):\n",
    "                if int(lst[k])>x:\n",
    "                    lst.insert(k,str(x))\n",
    "                    b+=1\n",
    "                    break\n",
    "            if b==0:\n",
    "                lst.append(str(x))\n",
    "\n",
    "        return \"\".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        length=len(n)\n",
    "        lst=[]\n",
    "        for i in range(length):\n",
    "            lst.append(n[i])\n",
    "        if n[0]!=\"-\":\n",
    "            a=0\n",
    "            for j in range(length):\n",
    "                if int(lst[j])<x:\n",
    "                    lst.insert(j,str(x))\n",
    "                    a+=1\n",
    "                    break\n",
    "            if a==0:\n",
    "                lst.append(str(x))\n",
    "        else:\n",
    "            b=0\n",
    "            for k in range(1,length):\n",
    "                if int(lst[k])>x:\n",
    "                    lst.insert(k,str(x))\n",
    "                    b+=1\n",
    "                    break\n",
    "            if b==0:\n",
    "                lst.append(str(x))\n",
    "\n",
    "        return \"\".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValue(self, n: str, x: int) -> str:\n",
    "        l = list(n)\n",
    "        if l[0] == '-':\n",
    "            for i, t in enumerate(l[1:]):\n",
    "                if x < int(t):\n",
    "                    l.insert(i+1, str(x))\n",
    "                    return \"\".join(l)\n",
    "            return n+str(x)\n",
    "        else:\n",
    "            for i, t in enumerate(l):\n",
    "                if x > int(t):\n",
    "                    l.insert(i, str(x))\n",
    "                    return \"\".join(l)\n",
    "            return n+str(x)\n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maxValue(self, n: str, x: int) -> str:\r\n",
    "        flag = False\r\n",
    "        if n[0]=='-':\r\n",
    "            new_n = list(n[1:])\r\n",
    "            flag =True\r\n",
    "        else:\r\n",
    "            new_n = list(n)\r\n",
    "        x = str(x)\r\n",
    "        if not flag:\r\n",
    "            i = 0\r\n",
    "            while i<len(new_n):\r\n",
    "                if new_n[i]>=x:\r\n",
    "                    i+=1\r\n",
    "                else:\r\n",
    "                    left = new_n[:i]\r\n",
    "                    right = new_n[i:]\r\n",
    "                    left.append(x)\r\n",
    "                    left.extend(right)\r\n",
    "                    return ''.join(left)\r\n",
    "            if i==len(new_n):\r\n",
    "                new_n.append(x)\r\n",
    "                return ''.join(new_n)\r\n",
    "        else:\r\n",
    "            i = 0\r\n",
    "            while i<len(new_n):\r\n",
    "                if new_n[i]<=x:\r\n",
    "                    i+=1\r\n",
    "                else:\r\n",
    "                    left = new_n[:i]\r\n",
    "                    right = new_n[i:]\r\n",
    "                    left.append(x)\r\n",
    "                    left.extend(right)\r\n",
    "                    return '-'+''.join(left)\r\n",
    "            if i== len(new_n):\r\n",
    "                new_n.append(x)\r\n",
    "                return '-'+''.join(new_n)\r\n",
    "            \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maxValue(self, n: str, x: int) -> str:\r\n",
    "        flag = False\r\n",
    "        if len(n)==0:\r\n",
    "            return str(x)\r\n",
    "        if n[0]=='-':\r\n",
    "            flag = True\r\n",
    "            new_n = list(n[1:])\r\n",
    "        else:\r\n",
    "            new_n = list(n)\r\n",
    "        x = str(x)\r\n",
    "        if not flag:\r\n",
    "            i = 0\r\n",
    "            while i<len(new_n):\r\n",
    "                if new_n[i]>=x:\r\n",
    "                    i+=1\r\n",
    "                else:\r\n",
    "                    res = new_n[:i]\r\n",
    "                    res.append(x)\r\n",
    "                    res.extend(new_n[i:])\r\n",
    "                    return ''.join(res)\r\n",
    "            res = new_n\r\n",
    "            res.append(x)\r\n",
    "            return ''.join(res)\r\n",
    "        else:\r\n",
    "            i = 0\r\n",
    "            while i<len(new_n):\r\n",
    "                if new_n[i]<=x:\r\n",
    "                    i+=1\r\n",
    "                else:\r\n",
    "                    res = new_n[:i]\r\n",
    "                    res.append(x)\r\n",
    "                    res.extend(new_n[i:])\r\n",
    "                    return '-'+''.join(res)\r\n",
    "            res = new_n\r\n",
    "            res.append(x)\r\n",
    "            return '-'+''.join(res)\r\n",
    "                    \r\n",
    "\r\n",
    "\r\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
